//
//  MSLChannelView.m
//  MSLNews
//
//  Created by lirenqiang on 16/6/12.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

#import "MSLChannelView.h"
#import "MSLNetworking.h"
#import "MSLChannelModel.h"
#import "MSLChannelViewCell.h"

static NSString * const MSLChannelViewCellId = @"MSLChannelViewCellId";

@interface MSLChannelView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation MSLChannelView {
    NSArray <MSLChannelModel *> * _channelModels;
}


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
 
    if (self) {
        
        self.backgroundColor = UIColor.greenColor;
        
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[MSLChannelViewCell class] forCellWithReuseIdentifier:MSLChannelViewCellId];
        
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;

        
        
        //load data
        dispatch_async(dispatch_get_main_queue(), ^{
           
            MSLNetworking * sharedNetworking = [MSLNetworking sharedNetworking];
            
            [sharedNetworking loadChannelViewData:^(id responseObject, NSError *error) {
                if (error) {
                    NSLog(@"%@", error);
                    return;
                }
                
                
                NSArray * tList = responseObject[@"tList"];
                NSMutableArray * arrM = [NSMutableArray arrayWithCapacity:tList.count];
                
                for (NSDictionary * dict in tList) {
                
                    MSLChannelModel * model = [MSLChannelModel channelModelWithDict:dict];
                    
                    [arrM addObject:model];
                    
                }
                
                _channelModels = [arrM copy];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:MSLChannelViewModelNotification object:_channelModels];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self reloadData];
                    [self selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
                });
                
            }];
            
        });
        
    }
    
    return self;
}


#pragma mark - data source
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _channelModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MSLChannelViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:MSLChannelViewCellId forIndexPath:indexPath];
    
    cell.tname= _channelModels[indexPath.item].tname;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * text = _channelModels[indexPath.item].tname;
    
    return [self sizeForLabel:text fontSize:channelFontS];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MSLChannelViewCell * cell = (MSLChannelViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    cell.tname = _channelModels[indexPath.item].tname;
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:MSLJumpNotification object:indexPath];
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MSLChannelViewCell * cell = (MSLChannelViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    cell.tname = _channelModels[indexPath.item].tname;

    
}



#pragma mark tool
- (CGSize)sizeForLabel:(NSString *)text fontSize:(CGFloat)fontSize {
    
    UILabel * label = [UILabel new];
    label.text = text;
    label.font = [UIFont systemFontOfSize:fontSize];
    [label sizeToFit];
    return label.bounds.size;
}


@end
